package kr.hkit.powerjava.chapter26.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import kr.hkit.powerjava.chapter26.DBProperties;

public class ConnectionFactory {
	private String driver;//com.mysql.jdbc.DRIVER
	private String url;
	private String user;
	private String password;
	
	private static ConnectionFactory instance= new ConnectionFactory();
	public static Connection getConnection(){
		return instance.createConnection();//외부에서는 이것만 보임
	}
	//여기에서만 가능, 외부에서 x
	private ConnectionFactory(){
		loadProperties();
		//이름으로 불러오자
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
		System.err.printf("%S","MySQL Driver not Found");
		System.exit(-1);//정상종료
		}
	}
	private Connection createConnection(){
		Connection con=null;
		//연결
		try {
			con=DriverManager.getConnection(url, user, password);
			System.out.println("MySQL Connection Success");
		} catch (SQLException e) {
		System.err.printf("%s","ERROR:Unable Connect to MySQL");
		}
		return con;//연결성공시
	}
	private void loadProperties() {
		Properties prop=DBProperties.loProperties("dbcon.xml");
		driver=prop.getProperty("DRIVER");
		url=prop.getProperty("URL");
		user=prop.getProperty("USER");
		password=prop.getProperty("PASSWORD");
	}
}
